package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.annotations.IgnoreLog;
import com.qf.annotations.LogOperationAnno;
import com.qf.dto.R;
import com.qf.dto.UserDto;
import com.qf.entity.TAdminUser;
import com.qf.entity.TBlackList;
import com.qf.service.BlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.swing.plaf.basic.BasicLabelUI;
import java.util.List;
import java.util.Map;

/*
杨威
2021/10/25
21:18
*/
@RequestMapping("/black")
@RestController
@Api(tags = "黑名单管理")
public class BalckListController {
    private BlackListService blackListService;
    @Autowired
    private void setBlackListService(BlackListService blackListService){
        this.blackListService=blackListService;
    }
    @GetMapping("/black/{id}")
    @ApiOperation("根据id查询黑名单")
    @IgnoreLog//当前注解是一个自定义注解 没有任何实际作用 用于区分是不是用记录日志
    public R findBlackById(@PathVariable Long id){
        TBlackList tBlackList = blackListService.findBlackById(id);
        return R.setOk(tBlackList);
    }
    @PostMapping("/black/{mobile}")
    @ApiOperation("新增黑名单")
    public R addBlack(@PathVariable String mobile, HttpSession session){
        TBlackList black=new TBlackList();
        TAdminUser user = (TAdminUser)session.getAttribute("user");
        if(user!=null){
            Long id = user.getId();
            Long type = user.getType();
            black.setCreater(id);
            black.setOwntype(type);
            black.setMobile(mobile);
        }

        blackListService.addBlack(black);
        return R.setOk();
    }
    @GetMapping("/all")
    @ApiOperation("分页查询所有黑名单数据")
    @LogOperationAnno("分页查询黑名单的数据")
    public R findAllByPage(@RequestParam(defaultValue = "1")@ApiParam(name = "pageNum",value = "页码") int pageNum, @RequestParam(defaultValue = "5") @ApiParam(name = "pageSize",value = "页大小") int pageSize){
        PageInfo<TBlackList> all = blackListService.findAll(pageNum, pageSize);
        return R.setOk(all);
    }
    @PutMapping("/black")
    @ApiOperation("修改黑名单")
    public R updateBlackById(@RequestBody TBlackList blackList,HttpSession session){
        TAdminUser user = (TAdminUser) session.getAttribute("user");
        if(user!=null){
            Long id = user.getId();
            Long type = user.getType();
            blackList.setCreater(id);
            blackList.setOwntype(type);
        }
        blackListService.udpateBlack(blackList);
        return R.setOk();
    }
    @DeleteMapping("/black")
    @ApiOperation("删除黑名单数据")
    public R deleteByIds(@RequestBody Map map){
        System.out.println(map);
        blackListService.deleteById((List<Integer>) map.get("ids"));
        return R.setOk();
    }
}
